package com.bikes.mileagetracker.persistence

import android.support.test.InstrumentationRegistry
import android.support.test.runner.AndroidJUnit4
import com.bikes.mileagetracker.orm.BaseDataBaseHelper
import com.bikes.mileagetracker.entity.UserEntity
import com.bikes.mileagetracker.service.PersistenceService
import com.bikes.mileagetracker.utils.TaskUtils
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith


@RunWith(AndroidJUnit4::class)
class UserEntityTest {

    val USERS = listOf<UserEntity>(
            UserEntity().apply {
                username = "wangds"
                status = 0
            },
            UserEntity().apply {
                username = "wujingc"
                status = 0
            },
            UserEntity().apply {
                username = "zhengw"
                status = 0
            },
            UserEntity().apply {
                username = "wangyt"
                status = 0
            }
    )

    val LOCK = Object()

    fun notifyLock() {
        synchronized(LOCK) {
            LOCK.notify()
        }
    }

    fun lock() {
        synchronized(LOCK) {
            LOCK.wait(60000)
        }
    }

    @Test
    fun testUserEntityOps() {

        val appContext = InstrumentationRegistry.getTargetContext()
        Assert.assertEquals("com.bikes.mileagetracker", appContext.packageName)

        BaseDataBaseHelper.createTable(BaseDataBaseHelper.getDatabase()!!, UserEntity::class.java, true)

        val helper = object : BaseDataBaseHelper<UserEntity>() {}

        while (PersistenceService.service == null) {
            PersistenceService.bind(appContext)
            Thread.sleep(100)
        }

        val service = PersistenceService.service!!

        TaskUtils.runInBackground {
            service.saveUser(USERS[0])
            notifyLock()
        }
        lock()

        var result = helper.byId(USERS[0].id)
        Assert.assertNotNull(result)
        Assert.assertNotNull(result!!.id)
        Assert.assertEquals(USERS[0].id, result!!.id)

        TaskUtils.runInBackground {
            service.saveUser(USERS[1])
            notifyLock()
        }
        lock()

        result = helper.byId(USERS[1].id)
        Assert.assertNotNull(result)
        Assert.assertNotNull(result!!.id)
        Assert.assertEquals(USERS[1].id, result!!.id)

        TaskUtils.runInBackground {
            service.saveUser(USERS[2])
            notifyLock()
        }
        lock()
        result = helper.byId(USERS[2].id)
        Assert.assertNotNull(result)
        Assert.assertNotNull(result!!.id)
        Assert.assertEquals(USERS[2].id, result!!.id)

        TaskUtils.runInBackground {
            service.saveUser(USERS[3])
            notifyLock()
        }
        lock()
        result = helper.byId(USERS[3].id)
        Assert.assertNotNull(result)
        Assert.assertNotNull(result!!.id)
        Assert.assertEquals(USERS[3].id, result!!.id)

        var users = service.loadUserList()
        Assert.assertEquals(4, users.size)

        TaskUtils.Companion.runInBackground {
            service.removeUser(USERS[0])
            notifyLock()
        }

        lock()

        users = service.loadUserList()
        Assert.assertEquals(3, users.size)

        users = helper.byCondition("", arrayOf(), "", "", "")
        Assert.assertEquals(4, users.size)

        result = helper.byId(USERS[0].id)

        Assert.assertEquals(USERS[0].id, result!!.id)
        Assert.assertNotEquals(result.status, 0)

        result = service.findUserByName(USERS[1].username)
        Assert.assertEquals(USERS[1].id, result!!.id)

        helper.delete(USERS[0])
        helper.delete(USERS[1])
        helper.delete(USERS[2])
        helper.delete(USERS[3])


    }
}